package com.ruoyi.project.business.domain.entity;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.project.business.domain.model.HardSoftScore;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.Solution;
import com.ruoyi.project.business.domain.model.month.EmpDailySchedule;
import com.ruoyi.project.business.domain.model.month.ShiftDefine;
import com.ruoyi.project.business.domain.rules.Rule;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

@Data
public class MonthScheduleEmp extends Emp {
    // 初始参数

    // 运行时参数
    private ShiftDefine shift;
    private List<ScheduleDefineMirror> scheduleDefines;


    /**
     * 月排班的结果
     */
    private List<EmpDailySchedule> shifts;


    public JudgeResult ifAssign(Solution solution) {
        List<Rule> rules = getRules();
        JudgeResult result = new JudgeResult();
        if (CollectionUtils.isEmpty(rules)) {
            result.setResult(Rule.RESULT_ACCEPT);
            result.mergeScore(HardSoftScore.defaultScore());
        } else {
            int size = scheduleDefines.size();
            for (Rule rule : rules) {
                rule.setShift(shift);
                JudgeResult judge = rule.judge(solution, this);
                if (judge.isAccept()) {
                    // 接受并继续
                    if(judge.getScheduleDefine() == null) {
                        result.setScheduleDefine(scheduleDefines.get(RandomUtil.randomInt(size)));
                    }
                    result.setTips(rule.getClass().getSimpleName() + "接受并继续");
                    result.setResult(Rule.RESULT_ACCEPT);
                    result.mergeScore(judge.getScore());
                } else if (judge.isStopTask()) {
                    // 终止任务
                    result.setTips(rule.getClass().getSimpleName() + "终止任务");
                    result.setResult(Rule.STOP_TASK);
//                return result;
                    break;
                } else if (judge.isStopRule()) {
                    // 拒绝但继续
                    result.setTips(rule.getClass().getSimpleName() + "拒绝但继续");
                    result.mergeScore(judge.getScore());
                    result.setResult(Rule.REJECT_AND_CONTINUE_TASK);
                    break;
                } else {

                }
            }
        }

        return result;
    }
}
